package com.lagou.edu.controller;

import com.lagou.edu.pojo.User;
import com.lagou.edu.pojo.UserVo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

/**
 * 绑定参数
 */
@Controller
@RequestMapping("demo")
public class ParamController {

    /**
     * 对 servlet api 的支持
     * http://localhost:8080/demo/handle10?id=10
     */
    @RequestMapping("handle10")
    public ModelAndView handle10(ModelAndView modelAndView, HttpServletRequest request, HttpServletResponse response){
        String id = request.getParameter("id");
        Date date = new Date();
        modelAndView.addObject("nowDate", date  + " id==>>" + id);
        modelAndView.setViewName("result");
        return modelAndView;
    }

    /**
     * 绑定简单类型参数
     * http://localhost:8080/demo/handle11?id=10
     */
    @RequestMapping("handle11")
    public ModelAndView handle11(ModelAndView modelAndView, Integer id){
        Date date = new Date();
        modelAndView.addObject("nowDate", date  + " id==>>" + id);
        modelAndView.setViewName("result");
        return modelAndView;
    }

    /**
     * @RequestParam 传递的参数名和声明的形参名称保持一致,不一致可用该注解
     * http://localhost:8080/demo/handle12?ids=10
     */
    @RequestMapping("handle12")
    public ModelAndView handle12(ModelAndView modelAndView, @RequestParam(value = "ids",defaultValue = "3") Integer id){
        Date date = new Date();
        modelAndView.addObject("nowDate", date  + " id==>>" + id);
        modelAndView.setViewName("result");
        return modelAndView;
    }

    /**
     * 绑定pojo类型参数
     * http://localhost:8080/demo/handle13?name=tom&age=13
     */
    @RequestMapping("handle13")
    public ModelAndView handle13(ModelAndView modelAndView, User user){
        Date date = new Date();
        modelAndView.addObject("nowDate", date  + " user ==>>" + user);
        modelAndView.setViewName("result");
        return modelAndView;
    }

    /**
     * 绑定pojo包装类型参数
     * http://localhost:8080/demo/handle14?id=14&user.name=Lucy&user.age=14
     */
    @RequestMapping("handle14")
    public ModelAndView handle14(ModelAndView modelAndView, UserVo userVo){
        Date date = new Date();
        modelAndView.addObject("nowDate", date  + " userVo ==>>" + userVo);
        modelAndView.setViewName("result");
        return modelAndView;
    }

    /**
     * 绑定日期类型参数
     * 定义类型转换器 DateConverter 并注册
     * http://localhost:8080/demo/handle15?birthday=2021-11-23
     */
    @RequestMapping("handle15")
    public ModelAndView handle15(ModelAndView modelAndView, Date birthday){
        modelAndView.addObject("nowDate", birthday);
        modelAndView.setViewName("result");
        return modelAndView;
    }

    /**
     * 绑定List类型参数
     */
    @RequestMapping("handle16")
    public ModelAndView handle16(ModelAndView modelAndView, UserVo userVo){
        Date date = new Date();
        modelAndView.addObject("nowDate", date  + " userVo ==>>" + userVo);
        modelAndView.setViewName("result");
        return modelAndView;
    }

    /**
     * 绑定Map类型参数
     */
    @RequestMapping("handle17")
    public ModelAndView handle17(ModelAndView modelAndView, UserVo userVo){
        Date date = new Date();
        modelAndView.addObject("nowDate", date  + " userVo ==>>" + userVo);
        modelAndView.setViewName("result");
        return modelAndView;
    }

}
